#!usr/bin/python
# -*- coding: utf-8 -*-
import requests
from flask import session, request
from config import Config
from app.get_token import get_token


def get_user(code):
    token = get_token()
    if code:
        result = requests.get(Config.GET_USERID_URL.format(ACCESS_TOKEN=token, CODE=code)).json()
        user_id = result.get('UserId')
        user_info = requests.get(Config.GET_USERINFO_URL.format(ACCESS_TOKEN=token, USERID=user_id)).json()
        user_name = user_info.get('name')
        user_tel = user_info.get('mobile')
        user_department = user_info.get('department')
        session['user_id'] = user_id
        session['user_name'] = user_name
        session['user_mobile'] = user_tel
        session['user_department'] = user_department
